package com.haoxiaozhang.hxz_app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.haoxiaozhang.hxz_app.IndexActivity;
import com.haoxiaozhang.hxz_app.R;
import com.haoxiaozhang.hxz_app.UserInfoHolder;
import com.haoxiaozhang.hxz_app.bean.Login;
import com.haoxiaozhang.hxz_app.bean.User;
import com.haoxiaozhang.hxz_app.business.LoginBusniess;
import com.haoxiaozhang.hxz_app.net.CommonCallback;
import com.haoxiaozhang.hxz_app.utils.T;

import java.util.List;

public class LoginActivity extends BaseActivity {

    EditText usernameEdit;
    EditText passwordEdit;
    Button loginButton;
    LoginBusniess loginBusniess = new LoginBusniess();

    private static final String KEY_OUID = "key_ouid";
    private static final String KEY_OESID = "key_oesid";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);
        initData();
        initView();
        initEvent();

    }

    //判断本地是否有存储用户信息,有的话直接进入首页
    private void initData() {

        User user =  UserInfoHolder.getInstance().getUser();

        if(user != null){
            Intent intent = new Intent(LoginActivity.this,IndexActivity.class);
            startActivity(intent);
        }
    }

    private void initEvent() {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                String username = usernameEdit.getText().toString();
                String password = passwordEdit.getText().toString();

                Log.d("TAG",username);
                Log.d("TAG",password);

                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
                    T.showToast("帐号或密码不能为空");
                    return;
                }

                startLoadingProgress();

                loginBusniess.login(username, password, new CommonCallback<List<Login>>() {
                    @Override
                    public void onError(Exception e) {
                        stopLoadingProgress();
                        T.showToast(e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<Login> response) {
                        stopLoadingProgress();
                        Log.d("TAG", String.valueOf(response));

                        //默认先获取第一个机构
                        Login login = response.get(0);

                        //将机构ID保存在本地缓存中
                        UserInfoHolder.getInstance().setUser(new User(login.getOu_id(),login.getOu_oesid()));

                        Intent intent = new Intent(LoginActivity.this,IndexActivity.class);
                        startActivity(intent);
                    }

                });


            }
        });
    }

    private void initView() {
        usernameEdit = findViewById(R.id.edit_username);
        passwordEdit = findViewById(R.id.edit_password);
        loginButton = findViewById(R.id.button_login);
    }

}
